package com.kdtech.analyse.app;
import java.util.HashMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.StringUtils;

/**
 * @链接 http://api.miaopai.com/m/v5_hot_channel.json
 * @名字 秒拍视频
 * @作者 dhy
 */
public class MiaopaiAppAnalyse implements AnalyseNews {
	
	public static HashMap<String, String> getLinks(String url){
		HashMap<String, String> links = new HashMap<String, String>();
		
//		UrlMeta html = CrawlHTML.responseToURL(url);
		UrlMeta html = null;
		if(html != null){
			String htmltxt = html.getHtml();
			if(StringUtils.isNotBlank(htmltxt)){
				JSONObject jObj = JSONObject.parseObject(htmltxt);
				if(jObj==null){
					return links;
				}
				JSONArray results = jObj.getJSONArray("result");
				if(results==null){
					return links;
				}
				for(int i = 0; i < results.size(); i++){
					JSONObject result = results.getJSONObject(i);
					JSONObject channel = result.getJSONObject("channel");
					if(channel==null){
						continue;
					}
					String scid = channel.getString("scid");
					if(scid==null){
						continue;
					}
					String link = "http://api.miaopai.com/m/v2_channel.json?fillType=255&scid=" + scid;
					links.put(link, "");
				}
			}
		}
		return links;
	}

	private static final String[] regex={
		"http://api.miaopai.com/m/v2_channel.json\\?fillType=255&scid=.*",
		
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		
		if(StringUtils.isNotBlank(htmltxt) && htmltxt.length() > 1000){
			try{
				JSONObject jObj = JSONObject.parseObject(htmltxt);
				JSONObject result = jObj.getJSONObject("result");
				JSONObject stream = result.getJSONObject("stream");
				JSONObject ext = result.getJSONObject("ext");
				JSONObject owner = ext.getJSONObject("owner");
				
				String scid = result.getString("scid");
				
				title = ext.getString("ft");
				if(StringUtils.isBlank(title)){
					title = ext.getString("t");
					if(StringUtils.isBlank(title)){
						title = result.getString("topicinfo");
					}
				}
				content = ext.getString("t");
				date = ext.getLong("finishTime");
				author = owner.getString("nick");
				url = stream.getString("base");
				if(url.endsWith("/stream/")){
					url = "http://bsyqncdn.miaopai.com/stream/"+scid+".mp4";
				}
			}catch(RuntimeException e){
				e.printStackTrace();
			}
		}
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);
		news.setUrl(url);

		return news;

	}

	

	

	
}

